package org.chenzhi.allinpaymvp.mvp.biz;

import android.text.TextUtils;

import java.util.Calendar;

/**
 * @author orange
 */

public class AgeBiz {

    //错误列表
    public static final int NOT_VALID_BIRTH = -1; //不是正确的出生年份
    public static final int AGE_TOO_LARGE = -2; //年龄过大异常
    public static final int HAVE_NOT_BORN = -3; //尚未出生
    public static final int AGE_MAY_TOO_LARGE = -4; //年龄超过两百岁时报

    public int calc(String yearString) {
        if (!TextUtils.isDigitsOnly(yearString)) {
            return NOT_VALID_BIRTH;
        }

        int year = 0;
        try {
            year = Integer.valueOf(yearString);
        } catch (NumberFormatException e) { //前面已经判断是否为数字格式，此处异常原因必定为超过Integer的长度
            return AGE_TOO_LARGE;
        }

        int nowYear = Calendar.getInstance().get(Calendar.YEAR);
        if (year > nowYear) {
            return HAVE_NOT_BORN;
        }

        int age = nowYear - year;
        if (age > 200) {
            return AGE_MAY_TOO_LARGE;
        }

        return age;
    }
}
